package com.huluobo.a2301mdmdemo.pages

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2301mdmdemo.MyNavHost
import com.huluobo.a2301mdmdemo.R
import com.huluobo.a2301mdmdemo.demo.SecondPage
import com.huluobo.apps.pages.AppsPage
import com.huluobo.document.pages.LocalFilePage
import com.huluobo.lib_base.RoutePath
import com.huluobo.report.pages.ReportCommitPage
import com.huluobo.report.pages.ReportPage
import com.huluobo.sign.pages.SignCommitPage
import com.huluobo.sign.pages.SignListPage
import com.huluobo.user.pages.LoginPage
import com.huluobo.user.pages.MinePage
import com.huluobo.user.pages.RegisterPage

/**
 *  Created by LC on 2025/6/23.
 *  框架页面,在MainActivity中不放任何控件,导航之类的东西都在当前页面
 */

sealed class NavItem(val route: String, val title: String, val img: Int) {
    data object MessagePage : NavItem(RoutePath.MESSAGE, "消息", R.drawable.message)
    data object HomePage : NavItem(RoutePath.HOME, "门户", R.drawable.home)
    data object MinePage : NavItem(RoutePath.MINE, "我的", R.drawable.mine)
}

@Composable
fun MainPage() {
    //底部导航的数据源
    val list = listOf(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)

    //导航控制器
    val navHostController = rememberNavController()

    //通过导航器获取当前回退栈并转化为状态
    val stackEntryState by navHostController.currentBackStackEntryAsState()

    //通过回退站获取所有的路由地址
    val destination = stackEntryState?.destination

    Scaffold(
        bottomBar = {
            //打开新页面的时候,需要判断地址,是否是首页的三个页面(message,home,mine)
            //如果是,则显示NavigationBar 如果否,就跳过NavigationBar不展示导航

            if (destination?.hierarchy?.any {
                    //通过拿到底部导航的地址(历史记录)来对比,如果
                    list.map { navItem ->
                        //map负责转化,只提取list中route的部分来进行对比
                        navItem.route
                        //it.route 从回退栈中所有的地址进行对比,如果包含就位true
                    }.contains(it.route)
                } == true) {
                NavigationBar {
                    //遍历底部导航数据源
                    list.forEach {
                        NavigationBarItem(selected = false, onClick = {
                            //底部导航点击时,切换对应的路由地址
                            navHostController.navigate(it.route)
                        }, icon = {
                            Icon(
                                painter = painterResource(id = it.img),
                                contentDescription = "",
                                modifier = Modifier.size(30.dp)
                            )
                        }, label = {
                            Text(text = it.title)
                        })
                    }
                }
            }
            //如果其中不包含首页3个页面的地址,就跳过展示底部导航
        }
    ) {
        MyNavHost(it = it, navHostController = navHostController)
    }
}